var o_auth = {};

var auth = new Function();
auth.prototype = o_auth;

o_auth.showLogin = function() {
	$.ligerDialog.open( {
		url : ROOT + '/auth/login-home.action',
		height : 200,
		width : 400,
		modal : true
	});

};
o_auth.loginReset = function() {
	$('#loginform')[0].reset();
	$('#loginform').resetForm();
};
o_auth.loginSubmit = function() {
	var username=$('#txtUserName').val();
	if(!username){
		alert("请输入用户名！");
		return;
	}
	var password=$('#txtPwd').val();
	if(!password){
		alert("请输入密码！");
		return;
	}
	$.ajax( {
		type : "POST",
		url : "login.action",
		data: "username="+username+"&password="+ escape(password)+"&isJson=true",
		success : function(obj) {
			if(obj.code=="0")
				alert(obj.msg);
			else
				window.location.reload(false);
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    alert("出错了："+textStatus);
		}
	});
	return;

};
o_auth.loginSubmit1 = function() {
	var form=$('#loginform1');
	var legal =form.validate();
	if(legal){
	form.submit();
	}
	return;

};
o_auth.logout=function(){
	$.ajax( {
		type : "POST",
		url : ROOT+"/auth/login-logout.action",
		success : function(msg) {
			//window.location.reload(false);
			window.location=ROOT+'/auth/login.action';
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    alert("出错了："+textStatus);
		}
	});
}
o_auth.addRole=function(){
	$.ligerDialog.prompt('新增角色', function(yes, value)
	{
		if (yes){
			$.ajax( {
		 		type : "POST",
		 		url : "auth-addRole.action",
		 		data : "role.name="+value,
		 		success : function(msg) {
					alert(msg.jsonResult);
		 			window.location.reload(false);
		 		},
		 		error:function (XMLHttpRequest, textStatus, errorThrown) {
		 		    alert("出错了："+errorThrown);
		 		}
		 	});
		}
	}); 
	
};

o_auth.editRole=function(id,name){
	$.ligerDialog.prompt('编辑角色',name, function(yes, value)
	{
		if (yes){
			$.ajax( {
		 		type : "POST",
		 		url : "auth-editRole.action",
		 		data : "role.name="+value+"&role.id="+id,
		 		success : function(msg) {
					alert(msg.jsonResult);
		 			window.location.reload(false);
		 		},
		 		error:function (XMLHttpRequest, textStatus, errorThrown) {
		 		    alert("出错了："+errorThrown);
		 		}
		 	});
		}
	}); 
	
};
o_auth.setFunc=function(){
	
}
o_auth.showChangePwd=function(userId){
	page(ROOT+'/auth/auth-showChangePwd.action?id='+userId);
}
o_auth.changePwd=function(userId){
	if(userId==""){
		alert("用户不能为空");
		return;
	}
        			var form=$('#addform');
        			var oldpassword=$('#oldpassword').val();
        			if(!oldpassword){
        				alert("原密码不能为空");
        				return;
        			}
        			var password=$('#password').val();
        			if(!password){
        				alert("新密码不能为空");
        				return;
        			}
        			var password1=$('#password1').val();
        			if(!password1){
        				alert("新密码不能为空");
        				return;
        			}
        			if(password1!=password){
        				alert("新密码输入不一致");
        				return;
        			}
        			$.ajax( {
        		 		type : "POST",
        		 		url : ROOT+'/auth/auth-changePwd.action',
        		 		data : form.serialize(),
        		 		success : function(msg) {
        					alert(msg.jsonResult);
        					if(msg.jsonResult=='保存成功')
        						window.location.href="login-logout.action";
        		 		},
        		 		error:function (XMLHttpRequest, textStatus, errorThrown) {
        		 		    alert("出错了："+errorThrown);
        		 		}
        		 	});
	
};
o_auth.showAdd=function(){
	$('<div id="add_user"></div>').appendTo($('body'));;
	$("#add_user").load(ROOT+'/auth/auth-showAddUser.action');
	$.ligerDialog.open({target:$("#add_user") , height: 180, width: 300,modal:true,buttons: [
        { text: '确定', onclick: function (item, dialog) {
        			var form=$('#addform');
        			var username=$('#username').val();
        			if(!username){
        				alert("不能为空");
        				return;
        			}
        			var password=$('#password').val();
        			if(!password){
        				alert("不能为空");
        				return;
        			}
        			
        			$.ajax( {
        		 		type : "POST",
        		 		url : "auth-addUser.action",
        		 		data : form.serialize(),
        		 		success : function(msg) {
        					alert(msg.jsonResult);
        					dialog.close();
        					window.location.reload(false);
        		 		},
        		 		error:function (XMLHttpRequest, textStatus, errorThrown) {
        		 		    alert("出错了："+errorThrown);
        		 		}
        		 	});
        		 	
        	} },
        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
}
o_auth.showEdit=function(id){
	$('<div id="add_user"></div>').appendTo($('body'));;
	$("#add_user").load(ROOT+'/auth/auth-showAddUser.action?id='+id);
	$.ligerDialog.open({target:$("#add_user") , height: 180, width: 300,modal:true,buttons: [
        { text: '确定', onclick: function (item, dialog) {
        			var form=$('#addform');
        			var username=$('#username').val();
        			if(!username){
        				alert("不能为空");
        				return;
        			}
        			var password=$('#password').val();
        			if(!password){
        				alert("不能为空");
        				return;
        			}
        			
        			$.ajax( {
        		 		type : "POST",
        		 		url : "auth-editUser.action",
        		 		data : form.serialize(),
        		 		success : function(msg) {
        					alert(msg.jsonResult);
        					dialog.close();
        					window.location.reload(false);
        		 		},
        		 		error:function (XMLHttpRequest, textStatus, errorThrown) {
        		 		    alert("出错了："+errorThrown);
        		 		}
        		 	});
        		 	
        	} },
        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
}
o_auth.editUser=function(id,username){
	$.ligerDialog.prompt('编辑用户',username, function(yes, value)
	{
		if (yes){
			$.ajax( {
		 		type : "POST",
		 		url : "auth-editUser.action",
		 		data : "user.username="+value+"&user.id="+id,
		 		success : function(msg) {
					alert(msg.jsonResult);
		 			window.location.reload(false);
		 		},
		 		error:function (XMLHttpRequest, textStatus, errorThrown) {
		 		    alert("出错了："+errorThrown);
		 		}
		 	});
		}
	}); 
	
};
o_auth.delRole=function(){
	var ids = "";
	$('#ROLE .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择要删除的记录!");
		return;
	}
	$.ligerDialog.confirm('确定删除?', function(yes)
	{
		if(yes){
			$.ajax( {
				type : "POST",
				url : "auth-delRole.action",
				data : "ids="+ids,
				success : function(msg) {
					alert(msg.jsonResult);
					window.location.reload(false);
				},
				error:function (XMLHttpRequest, textStatus, errorThrown) {
				    alert("出错了："+errorThrown);
				}
			});
		}
	});
};
o_auth.checkRole= function(obj) {
	if (obj.attr('checked') == true) {
		$('#ROLE .selectItem').attr('checked', 'checked');
	} else {
		$('#ROLE .selectItem').attr('checked', '');
	}
};

o_auth.checkUser= function(obj) {
	if (obj.attr('checked') == true) {
		$('#USER .selectItem').attr('checked', 'checked');
	} else {
		$('#USER .selectItem').attr('checked', '');
	}
};

o_auth.delUser=function(){
	var ids = "";
	$('#USER .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择要删除的记录!");
		return;
	}
	$.ligerDialog.confirm('确定删除?', function(yes)
	{
		if(yes){
			$.ajax( {
				type : "POST",
				url : "auth-delUser.action",
				data : "ids="+ids,
				success : function(msg) {
					alert(msg.jsonResult);
					window.location.reload(false);
				},
				error:function (XMLHttpRequest, textStatus, errorThrown) {
				    alert("出错了："+errorThrown);
				}
			});
		}
	});
};

/**
 * 设置功能权限
 */
o_auth.setRoleFuncs=function(rid){
	$('<div id="func"></div>').appendTo($('body'));;
	$("#func").load(ROOT+'/auth/auth-showFuncs.action?role.id='+rid);
	$.ligerDialog.open({target:$("#func") , height: 300, width: 300,modal:true,buttons: [
        { text: '确定', onclick: function (item, dialog) {
        			var manager = $("#tree_funcs").ligerGetTreeManager();
        		 	var check = manager.getChecked();
        		 	var ids = "";
        		 	for(var i=0;i<check.length;i++){
        		 		//alert(check[i].data.text);
        		 		if (ids == "")
        					ids = check[i].data.id;
        				else
        					ids = ids + "," + check[i].data.id;
        		 	}
        		 	if(ids==""){
        				alert("请选择记录!");
        				return;
        			}
        		 	$.ajax( {
        				type : "POST",
        				url : "auth-setFuncs.action",
        				data : "ids="+ids+"&role.id="+rid,
        				success : function(msg) {
        					alert(msg.jsonResult);
        					window.location.reload(false);
        				},
        				error:function (XMLHttpRequest, textStatus, errorThrown) {
        				    alert("出错了："+errorThrown);
        				}
        			});
        		 	dialog.close(); 
        		 	
        	} },
        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
};

o_auth.batchSetFunc=function(){
	var ids = "";
	$('#ROLE .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择记录!");
		return;
	}
	$('<div id="func"></div>').appendTo($('body'));;
	$("#func").load(ROOT+'/auth/auth-showFuncs.action?role.id=999');
	$.ligerDialog.open({target:$("#func") , height: 300, width: 300,modal:true,buttons: [
        { text: '确定', onclick: function (item, dialog) {
        			var manager = $("#tree_funcs").ligerGetTreeManager();
        		 	var check = manager.getChecked();
        		 	var tids = "";
        		 	for(var i=0;i<check.length;i++){
        		 		//alert(check[i].data.text);
        		 		if (tids == "")
        					tids = check[i].data.id;
        				else
        					tids = tids + "," + check[i].data.id;
        		 	}
        		 	if(tids==""){
        				alert("请选择记录!");
        				return;
        			}
        		 	$.ajax( {
        				type : "POST",
        				url : "auth-batchSetFunc.action",
        				data : "ids="+ids+"&fids="+tids,
        				success : function(msg) {
        					alert(msg.jsonResult);
        					window.location.reload(false);
        				},
        				error:function (XMLHttpRequest, textStatus, errorThrown) {
        				    alert("出错了："+errorThrown);
        				}
        			});
        		 	dialog.close(); 
        		 	
        	} },
        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
	
};

o_auth.setRoles=function(uid){
	$('<div id="setRoles"></div>').appendTo($('body'));;
	$("#setRoles").load(ROOT+'/auth/auth-showRoles.action?user.id='+uid);
	$.ligerDialog.open({target:$("#setRoles") , height: 300, width: 300,modal:true,buttons: [
        { text: '确定', onclick: function (item, dialog) {
        			var manager = $("#tree_roles").ligerGetTreeManager();
        		 	var check = manager.getChecked();
        		 	var ids = "";
        		 	for(var i=0;i<check.length;i++){
        		 		if (ids == "")
        					ids = check[i].data.id;
        				else
        					ids = ids + "," + check[i].data.id;
        		 	}
        		 	if(ids==""){
        				alert("请选择记录!");
        				return;
        			}
        		 	$.ajax( {
        				type : "POST",
        				url : "auth-setRoles.action",
        				data : "ids="+ids+"&user.id="+uid,
        				success : function(msg) {
        					alert(msg.jsonResult);
        					window.location.reload(false);
        				},
        				error:function (XMLHttpRequest, textStatus, errorThrown) {
        				    alert("出错了："+errorThrown);
        				}
        			});
        		 	dialog.close(); 
        		 	
        	} },
        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
};

o_auth.batchSetRole=function(){
	var ids = "";
	$('#USER .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择记录!");
		return;
	}
	$('<div id="setRoles"></div>').appendTo($('body'));;
	$("#setRoles").load(ROOT+'/auth/auth-showRoles.action?user.id=999');
	$.ligerDialog.open({target:$("#setRoles") , height: 300, width: 300,modal:true,buttons: [
        { text: '确定', onclick: function (item, dialog) {
        			var manager = $("#tree_roles").ligerGetTreeManager();
        		 	var check = manager.getChecked();
        		 	var tids = "";
        		 	for(var i=0;i<check.length;i++){
        		 		if (tids == "")
        					tids = check[i].data.id;
        				else
        					tids = tids + "," + check[i].data.id;
        		 	}
        		 	if(tids==""){
        				alert("请选择记录!");
        				return;
        			}
        		 	$.ajax( {
        				type : "POST",
        				url : "auth-batchSetRole.action",
        				data : "ids="+ids+"&rids="+tids,
        				success : function(msg) {
        					alert(msg.jsonResult);
        					window.location.reload(false);
        				},
        				error:function (XMLHttpRequest, textStatus, errorThrown) {
        				    alert("出错了："+errorThrown);
        				}
        			});
        		 	dialog.close(); 
        		 	
        	} },
        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
	
};

o_auth.setCates=function(uid){
	$('<div id="setCates"></div>').appendTo($('body'));;
	$("#setCates").load(ROOT+'/auth/auth-showCates.action?user.id='+uid);
	$.ligerDialog.open({target:$("#setCates") ,height: 300, width: 400,modal:true,buttons: [
        { text: '确定', onclick: function (item, dialog) {
        			var manager = $("#tree_cates").ligerGetTreeManager();
        		 	var check = manager.getChecked();
        		 	var ids = "";
        		 	for(var i=0;i<check.length;i++){
        		 		if (ids == "")
        					ids = check[i].data.id;
        				else
        					ids = ids + "," + check[i].data.id;
        		 	}
        		 	if(ids==""){
        				//alert("请选择记录!");
        				//return;
        			}
        		 	$.ajax( {
        				type : "POST",
        				url : "auth-setCates.action",
        				data : "ids="+ids+"&user.id="+uid,
        				success : function(msg) {
        					alert(msg.jsonResult);
        					window.location.reload(false);
        				},
        				error:function (XMLHttpRequest, textStatus, errorThrown) {
        				    alert("出错了："+errorThrown);
        				}
        			});
        		 	dialog.close(); 
        		 	
        	} },
        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
};

o_auth.setRoleCates=function(rid){
	$('<div id="setCates"></div>').appendTo($('body'));;
	$("#setCates").load(ROOT+'/auth/auth-showRoleCates.action?role.id='+rid);
	$.ligerDialog.open({target:$("#setCates") ,height: 300, width: 400,modal:true,buttons: [
        { text: '确定', onclick: function (item, dialog) {
        			var manager = $("#tree_cates").ligerGetTreeManager();
        		 	var check = manager.getChecked();
        		 	var ids = "";
        		 	for(var i=0;i<check.length;i++){
        		 		if (ids == "")
        					ids = check[i].data.id;
        				else
        					ids = ids + "," + check[i].data.id;
        		 	}
        		 	/*if(ids==""){
        				alert("请选择记录!");
        				return;
        			}*/
        		 	$.ajax( {
        				type : "POST",
        				url : "auth-setRoleCates.action",
        				data : "ids="+ids+"&role.id="+rid,
        				success : function(msg) {
        					alert(msg.jsonResult);
        					window.location.reload(false);
        				},
        				error:function (XMLHttpRequest, textStatus, errorThrown) {
        				    alert("出错了："+errorThrown);
        				}
        			});
        		 	dialog.close(); 
        		 	
        	} },
        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
};

o_auth.batchSetCate=function(){
	var ids = "";
	$('#USER .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择记录!");
		return;
	}
	$('<div id="setCates"></div>').appendTo($('body'));;
	$("#setCates").load(ROOT+'/auth/auth-showCates.action?user.id=999');
	$.ligerDialog.open({target:$("#setCates") , height: 300, width: 400,modal:true,buttons: [
        { text: '确定', onclick: function (item, dialog) {
        			var manager = $("#tree_cates").ligerGetTreeManager();
        		 	var check = manager.getChecked();
        		 	var tids = "";
        		 	for(var i=0;i<check.length;i++){
        		 		if (tids == "")
        					tids = check[i].data.id;
        				else
        					tids = tids + "," + check[i].data.id;
        		 	}
        		 	/*if(tids==""){
        				alert("请选择记录!");
        				return;
        			}*/
        		 	$.ajax( {
        				type : "POST",
        				url : "auth-batchSetCate.action",
        				data : "ids="+ids+"&cids="+tids,
        				success : function(msg) {
        					alert(msg.jsonResult);
        					window.location.reload(false);
        				},
        				error:function (XMLHttpRequest, textStatus, errorThrown) {
        				    alert("出错了："+errorThrown);
        				}
        			});
        		 	dialog.close(); 
        		 	
        	} },
        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
	
};

o_auth.search=function(){
	var f=$('#searchForm');
	f.submit();
};
